import pageination from '../../components/pageination/pageination.vue'
import urls from "../../common/js/api_url_array";
export default {
	data() {
			return {
				//数据
				total: 0, //总条数
				page: 1, //当前页
				size: 10, //每页显示条数
				search: '', //搜索信息
				validate: {}, //验证对象
				searchType: 'name', //搜索类型
				technologicalInformation: {}, //科技信息
				technologicalInformationList: [], //科技信息列表
				ApplicationStatus: false,
				Application: false,
				curId: 0,
				uploadNum: 0,
				isModify:false,//true 修改  false新增
			}
		},
		components: {
			pageination
		},
		created() { //调用方法
		},
		mounted() {
			let that = this;
			//校验名称重复
			$.validator.addMethod("checkNameRepeat", function(value, element, params) {
				let flg = false;
				if (that.technologicalInformation.id != null && (value == that.technologicalInformation.name)) {
					flg = true;
				} else {
					$.ajax({
						type: "POST",
						async: false,
						url: that.urls.checkTechnologicalInformationName,
						data: JSON.stringify({
							"name": value
						}),
						dataType: "json",
						contentType: "application/json",
						success: function(data) {
							if (data.success) {
								flg = !data.data;
							}
						}
					});
				}
				return this.optional(element) || flg;
			}, "该名称已存在！");

			//校验公开号
			$.validator.addMethod("checkOvertNum", function(value, element, params) {
				let reg = /^[0-9a-zA-Z]{0,20}$/;
				return this.optional(element) || value.match(reg);
			}, "公开号只能输入数字、字母最大20字符！");

			//校验电话
			$.validator.addMethod("checkPhone", function(value, element, params) {
				let reg = /^1[3456789]\d{9}$/;
				return this.optional(element) || value.match(reg);
			}, "请输入合法电话！");

			//加载表单验证
			that.validate = $("#technologicalInformationForm").validate({
				ignore: [],
				rules: {
					name: { // 成果名称
						required: true,
						maxlength: 50,
						checkNameRepeat: true
					},
					applicant: { // 申请人
						required: true,
						maxlength: 10
					},
					overtNum: { // 公开号
						required: true,
						checkOvertNum: true
					},
					phone: { // 联系方式
						required: true,
						checkPhone: true
					},
					introduction: { // 成果简介
						maxlength: 1000
					}
				},
				messages: {
					name: { // 成果名称
						required: "请输入名称",
						maxlength: "名称最多只能输入50个字"
					},
					applicant: { // 申请人
						required: "请输入申请人",
						maxlength: "申请人最多只能输入10个字"
					},
					overtNum: { // 公开号
						required: "请输入公开号"
					},
					phone: { // 联系方式
						required: "请输入联系方式"
					},
					introduction: { // 成果简介
						maxlength: "简介最多只能输入1000个字"
					}
				},
				//			errorElement: "em",
				//			errorPlacement: function(error, element) {
				//				error.appendTo(element.parents(".error-box").children(".error-msg"));
				//			}
			});
		},
		methods: { //写函数方法
			//页码切换执行方法
			pageFn: function(val, pid) {
				this.page = parseInt(val);
				//此处调列表数据接口
				this.searchTodo();
			},
			/**返回子组件用户选择的一页显示条数**/
			obtainChild: function(data) {
				this.row = data;
			},
			//人工录入及修改模态框
			manually: function(id) {
				//清空表单数据和校验信息
				$("#technologicalInformationForm").validate().resetForm();
				$("input[type='reset']").trigger("click");
				this.getData(id);
				this.ApplicationStatus = true;
				if(id){
					this.isModify=true;
				}else{
					this.isModify=false;
				}
			},
			//查看
			toView: function(id) {
				this.getData(id);
				this.Application = true;
			},
			//关闭模态框
			closeModel: function() {
				this.Application = false;
				this.ApplicationStatus = false;
			},
			touchOffIdInput(n) {
				this.uploadNum = n;
				if (n == 1) {
					$("#handIdFront").click();
				}
			},
			idFrontImg: function() {
				var _this = this;
				var picFile = document.getElementById("handIdFront").files[0];
				//创建读取文件的对象
				var reader = new FileReader();
				//创建文件读取相关的变量
				var imgFile;
				reader.readAsDataURL(picFile);
				//为文件读取成功设置事件
				reader.onload = function(e) {
					imgFile = e.target.result;
					$(".showHandIdFront").attr("src", imgFile);
					$(".handIdFrontA").css("display", "none");
				};
			},
			//搜索数据列表
			searchTodo: function() {
				let that = this;
				let obj = {};
				obj["pageNum"] = that.page;
				obj["pageSize"] = that.size;
				obj[that.searchType] = that.search;
				that.$post(
						that.urls.getTechnologicalInformationPageInfo,
						obj
					)
					.then(function(res) {
						if (res.success) {
							that.technologicalInformationList = res.pageInfo.list;
							that.total = res.pageInfo.total;
							that.page = res.pageInfo.pageNum;
						}
					})
			},
			//保存
			saveData: function() {
				let that = this;
				let flg = that.validate.form();
				if (flg) {
					let dataObj = combinationFormObject(that.validate.currentForm);
					that.$post(
							that.urls.addTechnologicalInformation,
							dataObj
						)
						.then(function(res) {
							let msg = "";
							if (res.success) {
								msg = "保存成功！";
								that.searchTodo(); //重新渲染数据
								that.ApplicationStatus = false; //关闭窗口
							} else {
								msg = "保存失败！";
							}
							layer.msg(msg, {
								time: 3000, //3s后自动关闭
							});
						})
				}
			},
			//修改
			updateData: function(uuid) {
				let that = this;
				let flg = that.validate.form();
				if (flg) {
					let dataObj = combinationFormObject(that.validate.currentForm);
					that.$post(
							that.urls.updateTechnologicalInformation,
							dataObj
						)
						.then(function(res) {
							let msg = "";
							if (res.success) {
								msg = "修改成功！";
								that.searchTodo(); //重新渲染数据
								that.ApplicationStatus = false; //关闭窗口
							} else {
								msg = "修改失败！";
							}
							layer.msg(msg, {
								time: 3000, //3s后自动关闭
							});
						})
				}
			},
			//获取科技信息
			getData: function(id) {
				let that = this;
				that.technologicalInformation = {};
				if (id != null) {
					that.$post(
							that.urls.getTechnologicalInformation, {
								"id": id
							}
						)
						.then(function(res) {
							if (res.success) {
								that.technologicalInformation = res.data;
							}
						})
				}
			},
			//删除
			deleteData: function(id) {
				let that = this;
				layer.confirm('你确定要删除吗?', {
					icon: 3,
					title: '提示'
				}, function(index) {
					if (id != null) {
						//初始化政策发布分页信息
						that.$post(
								that.urls.deleteTechnologicalInformation, {
									"id": id
								}
							)
							.then(function(res) {
								let msg = "";
								if (res.success) {
									msg = '删除科技信息成功！';
									//重新渲染数据
									that.searchTodo();
								} else {
									msg = '删除科技信息失败！';
								}
								layer.msg(msg, {
									time: 3000, //3s后自动关闭
								});
							})
					}
				});
			},
			//导入数据
			inputData: function(event) {
				var that = this;
				var file = event.currentTarget.files[0];
				if (file == null) {
					return;
				}
				let formData = new FormData();
				formData.append('fileName', file);
				let config = {
					headers: {
						'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
					}
				};
				that.$post(
						that.urls.importTechnologicalInformation,
						formData,
						config
					)
					.then(function(res) {
						let msg = "";
						if (res.success) {
							msg = "导入数据成功！";
							that.searchTodo(); //重新渲染数据
						} else {
							msg = "导入数据失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			},
			//导出数据
			exportData: function() {
				window.location.href = this.urls.exportTechnologicalInformation + "?" + this.searchType + "=" + this.search;
			}
		}
}